package com.cxl.live.base;

import com.cxl.live.system.dto.UserInfo;
import com.cxl.live.util.DateUtil;
import com.fasterxml.jackson.annotation.*;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import javax.persistence.Transient;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: BaseDTO
 * 基础实体类
 * @Author: ChenXueLi
 * @Date: 2018-11-10 12:52
 */
@Data
public class BaseDTO implements Serializable {

    public interface Field{
        String OPERATE = "operate";
        String OBJECT_VERSION = "version";
        String CREATE_USER_ID = "createUserId";
        String CREATE_USER_NAME = "createUserName";
        String CREATE_TIME = "createTime";
        String MODIFY_USER_ID = "modifyUserId";
        String MODIFY_USER_NAME = "modifyUserName";
        String MODIFY_TIME = "modifyTime";
    }

    @ApiModelProperty(value = "操作类型，add/update/delete")
    @Transient
    private String _operate;

    @ApiModelProperty(value = "数据版本号,每发生update则自增,用于实现乐观锁", name = "version")
    private Long version;

    @ApiModelProperty(value = "备注", name = "remark")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String remark;

    /**
     * 下面是标准 WHO 字段
     */
    @ApiModelProperty(value = "创建人用户名", name = "createUserId")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer createUserId;

    @ApiModelProperty(value = "创建人名称", name = "createUserName")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @Transient
    private String createUserName;

    @ApiModelProperty(value = "创建时间", name = "createTime")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonFormat(pattern = DateUtil.DEFAULT_PATTERN, timezone = "GMT+8")
    private Date createTime;

    @ApiModelProperty(value = "更新人用户名", name = "modifyUserId")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer modifyUserId;

    @ApiModelProperty(value = "更新人名称", name = "modifyUserName")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @Transient
    private String modifyUserName;

    @ApiModelProperty(value = "更新时间", name = "modifyTime")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonFormat(pattern = DateUtil.DEFAULT_PATTERN, timezone = "GMT+8")
    private Date modifyTime;

    @ApiModelProperty(value = "是否存在", name = "isDeleted")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer isDeleted;

    @ApiModelProperty(value = "其它属性", name = "innerMap")
    @JsonIgnore
    @Transient
    protected Map<String, Object> innerMap = new HashMap<>();

    // 下面是扩展属性字段

//    @JsonInclude(JsonInclude.Include.NON_NULL)
//    private String attribute1;
//
//    @JsonInclude(JsonInclude.Include.NON_NULL)
//    private String attribute2;
//
//    @JsonInclude(JsonInclude.Include.NON_NULL)
//    private String attribute3;
//
//    @JsonInclude(JsonInclude.Include.NON_NULL)
//    private String attribute4;
//
//    @JsonInclude(JsonInclude.Include.NON_NULL)
//    private String attribute5;
//
//    @JsonInclude(JsonInclude.Include.NON_NULL)
//    private String attribute6;
//
//    @JsonInclude(JsonInclude.Include.NON_NULL)
//    private String attribute7;
//
//    @JsonInclude(JsonInclude.Include.NON_NULL)
//    private String attribute8;
//
//    @JsonInclude(JsonInclude.Include.NON_NULL)
//    private String attribute9;
//
//    @JsonInclude(JsonInclude.Include.NON_NULL)
//    private String attribute10;

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }

    private String toJsonString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
    }

    @JsonAnyGetter
    public Object getAttribute(String key) {
        return innerMap.get(key);
    }

    @JsonAnySetter
    public void setAttribute(String key, Object obj) {
        innerMap.put(key, obj);
    }

    /**
     * 初始化信息
     * @param userInfo
     */
    public void initModel(UserInfo userInfo) {
        this.create(userInfo);
        this.modify(userInfo);
        this.isDeleted = 1;
        this.version = 0L;
    }

    /**
     * 初始化信息
     */
    public void initModel() {
        this.createTime = new Date();
        this.modifyTime = new Date();
        this.isDeleted = 1;
        this.version = 0L;
    }

    /**
     * 设置创建信息
     * @param userInfo
     */
    public void create(UserInfo userInfo) {
        this.createTime = new Date();
        this.createUserId = userInfo.getFkUserId();
        this.createUserName = userInfo.getInfoName();
    }

    /**
     * 设置修改信息
     * @param userInfo
     */
    public void modify(UserInfo userInfo){
        this.modifyTime = new Date();
        this.modifyUserId = userInfo.getFkUserId();
        this.modifyUserName = userInfo.getInfoName();
    }

}
